﻿/*
创建人：longb
日期：2022-05-20
描述：收料缺件统计js
*/
var page = {
    pagetotal: 0,
    pageSize: 20,
    pageNumber: 1,
    editIndex: null, //easyUI单元格编辑
    rootPath: getRootPath(), //网站根路径
    ajaxUrl: getRootPath() + "/Ashx/ZsHandler/ZSReciveMaterialConfirmHandler.ashx",
    //初始化页面
    init: function () {
        page.bind();
        customizedEasyuiPagination('pp'); //定制化分页
    },
    //页面事件绑定
    bind: function () {
        $('#OrderType').combobox({
            data: [{ id: 1, text: '正常' }, { id: 0, text: '临时'}],
            valueField: 'id',
            textField: 'text'
        });
        $('#Status').combobox({
            data: [{ id: 0, text: '未生成' }, { id: 1, text: '已生成'}],
            valueField: 'id',
            textField: 'text'
        });
        $('#OrderType').combobox('setValue', 1);
        $('#Status').combobox('setValue', 1);
        //任务单信息
        $("#gdContainerInfo").datagrid({
            singleSelect: true,
            onLoadSuccess: function (data) {
                $('#pp').pagination('refresh', {	// 改变选项，并刷新分页栏信息
                    total: page.pagetotal,
                    pageSize: page.pageSize,
                    pageNumber: page.pageNumber
                });
                customizedEasyuiPagination('pp'); //定制化分页
            },
            onSelect: function (index, rowData) { page.GetMaterialData(); },
            onUnselect: function (index, rowData) { page.GetMaterialData(); },
            columns: [[
            //     { field: 'ck', title: '选择', checkbox: true },
            //     {field: 'ATTRIBUTEVALUE', title: '发动机号', width: 120 },
                 { field: 'MFGORDERNAME', title: '订单号', width: 150 },
		         { field: 'TOGETHERFLAG', title: '任务单号', width: 180 },
		         { field: 'WORKFLOWTYPE', title: '任务类型', width: 130 }, //--
                 {field: 'PRODUCTNAME', title: '件号', width: 120 },
                 { field: 'DESCRIPTION', title: '件号名称', width: 90 },
                 { field: 'WFDESC', title: '工艺名称', width: 120 },
                 { field: 'PLANNEDSTARTDATE', title: '计划开始时间', width: 130 },
                 { field: 'PLANNEDFINISHDATE', title: '计划完成时间', width: 130 },
            //     { field: 'TASKBILLSTATUS', title: '任务单状态', width: 100 },
                 { field: 'WORKFLOWSTEPNAME', title: '阶段', width: 100 },
                 { field: 'QTL', title: '预齐套率', width: 90, align: 'center', formatter: function (value, row, index) { if (value >= 1) { return '<span style="color:#0000ff">' + parseInt(value * 100) + '%</span>'; } else { return '<span style="color:red">' + (parseInt(value * 100)) + '%</span>'; } } }
	           ]]
        });

        //物料信息
        $('#MaterialTable').datagrid({
            checkOnSelect: false,
            selectOnCheck: false,
            singleSelect: true,
            columns: [[
                    //{ field: 'ck', title: '选择', checkbox: true },
		            { field: 'PRODUCTNAME', title: '件号', width: 100 },
		            { field: 'PRODUCTDESC', title: '件号名称', width: 200 },
                    { field: 'PARTTYPE', title: '零件类型', width: 100, align: 'center', formatter: function (value, row, index) { if (value == "0") { return '大件'; } else { return '小件'; } } },
		            { field: 'FIRSTASSEMBLEQTY', title: '单台数量', width: 100, align: 'center' },
                    { field: 'PRODUCTBATCHNO', title: 'ERP批次', width: 100, align: 'center' },
                    { field: 'ISSUEQTY', title: '装配数量', width: 100, align: 'center', hidden: true },
                    //{ field: 'CONTAINERNAME', title: '批次号', width: 200, align: 'center' },
                    { field: 'ONECONFIRMQTY', title: '已收料数量', width: 90, align: 'center' },
                    { field: 'TOTALCOLLECTQTY', title: '已收料数量', width: 90, align: 'center', hidden: true },
                    { field: 'NOTOTALCOLLECTQTY', title: '未收料数量', width: 90, align: 'center', formatter: function (value, row, index) { var c = row.ISSUEQTY - row.ONECONFIRMQTY; if (c < 0) { return 0; } else { return c; } } },
                    { field: 'CONTAINERNAME1', title: '预齐套率', width: 90, align: 'center', formatter: function (value, row, index) { var c = row.TOTALCOLLECTQTY / row.FIRSTASSEMBLEQTY; if (c >= 1) { return '<span style="color:#0000ff">' + parseInt(c * 100) + '%</span>'; } else { return '<span style="color:red">' + parseInt(c * 100) + '%</span>'; } } },
                ]],
            onCheck: function (index, rowData) { },
            onUncheck: function (index, rowData) { },
            onCheckAll: function (index, rowData) { },
            onUncheckAll: function (index, rowData) { },
            onClickCell: function (index, field, value) {
                if (value > 0) {
                    if (page.endEditing('MaterialTable')) {
                        $('#MaterialTable').datagrid('editCell', { index: index, field: field });
                        page.editIndex = index;
                    }
                }
            },
            onAfterEdit: function (index, row, changes) {
                //                //                console.info(changes);
                //                var ChangeQTY = Number(changes.ChangeQTY);
                //                if ((changes.ChangeQTY == "" || changes.ChangeQTY == undefined)) { ChangeQTY = Number(row.ISSUEQTY) - Number(row.ONECONFIRMQTY); }
                //                if (row.ChangeQTY != "") { ChangeQTY = row.ChangeQTY; }
                //                $('#MaterialTable').datagrid('updateRow', { index: index, row: { ChangeQTY: ChangeQTY} });
                if (row.ISSUEQTY < Number(changes.ChangeQTY) + Number(row.ONECONFIRMQTY)) { displayMessage("本次收料数量+已收料数量>装配数量", false); }

            }
        });
        //分页事件
        $('#pp').pagination({
            onSelectPage: function (pageNumber, pageSize) {
                customizedEasyuiPagination('pp'); //定制化分页
                page.pageSize = pageSize; page.pageNumber = pageNumber;
                page.GetTaskOrderData();
            }
        });
        //绑定分页go按钮
        $('#pageGo').on("click", function () {
            var pageNumber = $(this).parents('tr').find('.pagination-num').val();
            page.pageNumber = pageNumber;
            page.GetTaskOrderData();
        });
        //清除按钮
        $('#btnReset').on("click", function () {
            page.reset(true, true);
            page.pageNumber = 1; page.pagetotal = 0
            $('#pp').pagination('refresh', {
                total: page.pagetotal,
                pageSize: page.pageSize,
                pageNumber: page.pageNumber
            });
        });


    },
    //查询
    searchData: function () {
        page.reset(false, true);
        page.pageNumber = 1;
        $('#MaterialTable').datagrid('loadData', []);
        page.GetTaskOrderData();
    },
    //查询任务单信息
    GetTaskOrderData: function () {
        $('#MaterialTable').datagrid('loadData', [])
        var frdata = $('#searchForm').serializeArray();
        var data = {};
        $.each(frdata, function () {
            data[this.name] = this.value
        });
        var param = {
            action: "GetTaskOrderData",
            pageSize: page.pageSize,
            pageNumber: page.pageNumber,
            data: JSON.stringify([data])
        };
        page.post(param, function (res) {
            if (res.Result) {
                //                console.info(res);
                page.pagetotal = res.RowCount;
                $('#gdContainerInfo').datagrid('loadData', res.DBTable)
            }
        });
    },
    //物料信息
    GetMaterialData: function () {
        $('#MaterialTable').datagrid('loadData', []);
        var frdata = $('#searchForm').serializeArray();
        var data = {};
        $.each(frdata, function () {
            data[this.name] = this.value
        });
        var strNoList = [];
        var strIDList = [];
        var CheckedRows = $('#gdContainerInfo').datagrid('getChecked');
        $.each(CheckedRows, function (index, item) {
            strNoList.push(item.TOGETHERFLAG);
            strIDList.push(item.CONTAINERID);
        })
        var param = {
            action: "GetMaterialData",
            strNoList: strNoList.join(','),
            strIDList: strIDList.join(','),
            data: JSON.stringify([data])
        };
        if (strNoList.length == 0) {
            //displayMessage("请选择任务单");
            return;
        }
        page.post(param, function (res) {
            if (res.Result) {
                //                console.info(res);
                $('#MaterialTable').datagrid('loadData', res.DBTable)
            }
            //            displayMessage(res.Message, res.Result);
        });

    },
    //查询重置
    searchReset: function () {
        $('#searchForm').form('clear');
        $('#OrderType').combobox('setValue', 1);
        $('#Status').combobox('setValue', 1);
    },
    //批量报工
    BatchReciveMaterialCofirm: function (index) {
        var rows = $('#MaterialTable').datagrid('getChecked');
        if (index != null) {
            rows = [$('#MaterialTable').datagrid('getRows')[index]];
        }
        //        console.info(rows);
        var CheckedRows = $('#gdContainerInfo').datagrid('getSelected'); 
        var param = {
            action: "ReciveMaterialCofirm",
            type: page.pageType,
            data: JSON.stringify(rows),
            CONTAINERID: CheckedRows.CONTAINERID
        };
        if (rows.length == 0) {
            displayMessage("请先选择对应物料信息，再进行确认");
            return;
        }
        page.post(param, function (res) {
            if (res.Result) {
                page.GetMaterialData();
            }
            displayMessage(res.Message, res.Result);
        });

    },
    //清除方法，重要！！！
    reset: function (iscondition, isresult) {
        clearMessage();
    },
    //post数据
    post: function (param, callback) {
        $.ajax({
            type: "Post",
            url: page.ajaxUrl,
            data: param,
            cashe: false,
            async: true,
            dataType: "json",
            beforeSend: function () {
                $.messager.progress({ text: '', msg: '处理中...' });
            },
            success: function (data) {
                $.messager.progress('close');
                callback(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.progress('close');
                displayMessage(XMLHttpRequest.responseText, false);
            }
        });
    }
}
$(function () {
    page.init();
});